<?php
/**
 * 发送邮件类库
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-04-22 12:08:09
 * @version $Id$
 */
namespace phpmailer;

use think\Exception;
use phpmailer\PHPMailer;

class Email
{
    /**
     * 发送邮件
     * @param  string $to      发送给谁xxx@xxx.com
     * @param  string $title   发送的标题
     * @param  string $content 发送的内容
     * @return bool
     */
    public static function send($to, $title, $content)
    {
        date_default_timezone_set('PRC');//set time
        if (empty($to)) {
            return FALSE;
        }
        try {
            //Create a new PHPMailer instance
            $mail = new PHPMailer;
            //Tell PHPMailer to use SMTP
            $mail->isSMTP();
            //Enable SMTP debugging
            // 0 = off (for production use)
            // 1 = client messages
            // 2 = client and server messages
            // $mail->SMTPDebug = 2; // 在正式启用的时候需要关闭这个调试
            //Ask for HTML-friendly debug output
            // $mail->Debugoutput = 'html';
            //Set the hostname of the mail server
            $mail->Host = config('email.host');// 你的邮箱服务商
            //Set the SMTP port number - likely to be 25, 465 or 587
            $mail->Port = config('email.port'); // 邮箱端口
            //Whether to use SMTP authentication
            $mail->SMTPAuth = true; // 开启服务验证
            //Username to use for SMTP authentication
            $mail->Username = config('email.username'); // 你的邮箱地址
            //Password to use for SMTP authentication
            $mail->Password = config('email.password'); // 给予的STMP密码
            //Set who the message is to be sent from
            // $mail->setFrom(config('email.username'), 'FirstName LastName'); //发件人设置
            $mail->setFrom(config('email.username'), 'rejeo'); //发件人设置
            //Set an alternative reply-to address
            //$mail->addReplyTo('replyto@example.com', 'First Last');
            //Set who the message is to be sent to
            // $mail->addAddress($to, 'John Doe'); // 发送给谁
            $mail->addAddress($to);
            //Set the subject line
            $mail->Subject = $title; // 发送的邮件标题
            //Read an HTML message body from an external file, convert referenced images to embedded,
            //convert HTML into a basic plain-text alternative body
            $mail->msgHTML($content); // 发送的邮件内容
            //Replace the plain text body with one created manually
            //$mail->AltBody = 'This is a plain-text message body';
            //Attach an image file
            //$mail->addAttachment('images/phpmailer_mini.png');

            //send the message, check for errors
            if (!$mail->send()) {
                return FALSE;
                // echo "Mailer Error: " . $mail->ErrorInfo;
            } else {
                return TRUE;
                // echo "Message sent success!";
            }
        } catch (phpmailerException $e) {
            return FALSE;
        }
    }
}
